/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsf;
import ejb.Company;
import bean.CompanyBean;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
/**
 *
 * @author longdm
 */

public class CompanyController {

    /** Creates a new instance of CompanyController */
    public CompanyController() {
        companyDataModel = new ListDataModel();
    }

    private Company currentCompany;
    private ListDataModel companyDataModel;

    public ListDataModel getCompanyDataModel() {
        CompanyBean cb = new CompanyBean();
        companyDataModel.setWrappedData(cb.getAllCompany());
        return companyDataModel;
    }

    public Company getCurrentCompany() {
        return currentCompany;
    }

    public String editCompany()
    {
        CompanyBean cb = new CompanyBean();
        cb.editCompany(currentCompany);
        return "companyManager";
    }

    public String deleteCompany()
    {
        currentCompany = (Company) companyDataModel.getRowData();
        if(currentCompany.getWarehouseCollection().size()==0)
        {
            CompanyBean cb = new CompanyBean();
            cb.removeCompany(currentCompany);
        }
        else
        {
            FacesMessage message = new FacesMessage("Can not delete:", "There are still some warehouse");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            FacesContext.getCurrentInstance().addMessage("delete_error", message);
        }
        return "";
    }

    public String newCompany()
    {
        CompanyBean cb = new CompanyBean();
        cb.createCompany(currentCompany);
        return "companyManager";
    }

    public String toCompanyEdit()
    {
        currentCompany = (Company) companyDataModel.getRowData();
        return "companyEdit";
    }

    public String toNewCompany()
    {
        currentCompany = new Company();
        return "newCompany";
    }
}
